package com.yanqu.road.dao.impl.nenmonopolymarket.office;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.office.NewMonopolyMarketOfficeSeatData;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.office.NewMonopolyMarketOfficeSeatInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewMonopolyMarketOfficeSeatDataDaoImpl extends TempDataObjectDao<NewMonopolyMarketOfficeSeatData> {

    public static String getNewMonopolyMktOfficeSeatDataSql = "select * from `t_c_new_monopoly_market_office_seat` where `weekId`=? and `systemGroupId`=?";
    public Map<Integer, NewMonopolyMarketOfficeSeatInfo> getNewMonopolyMktOfficeSeatData(int weekId, int systemGroupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, NewMonopolyMarketOfficeSeatInfo> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewMonopolyMktOfficeSeatDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, systemGroupId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketOfficeSeatData data = parse(rs);
                map.put(data.getSeatId(), new NewMonopolyMarketOfficeSeatInfo(data));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewMonopolyMktOfficeSeatDataSql = "update `t_c_new_monopoly_market_office_seat` set seatLv=?,playerId=?,sitTime=?,addition=?,decorationClass=?,decorationId=?,decorationOverTime=?,changeTime=? where weekId=? and systemGroupId=? and seatId=?";
    @Override
    protected boolean updateData(NewMonopolyMarketOfficeSeatData newMonopolyMktOfficeSeatData) {
        newMonopolyMktOfficeSeatData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSeatLv()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMktOfficeSeatData.getPlayerId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSitTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getDecorationClass()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getDecorationId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getDecorationOverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getChangeTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSystemGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSeatId()));
        if (getDb().executeNoneQuery(updateNewMonopolyMktOfficeSeatDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewMonopolyMktOfficeSeatDataSql = "insert into `t_c_new_monopoly_market_office_seat`(weekId,systemGroupId,seatId,seatLv,playerId,sitTime,addition,decorationClass,decorationId,decorationOverTime,changeTime) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(NewMonopolyMarketOfficeSeatData newMonopolyMktOfficeSeatData) {
        newMonopolyMktOfficeSeatData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSystemGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSeatId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSeatLv()));
        para.put(i++, new DbParameter(Types.BIGINT, newMonopolyMktOfficeSeatData.getPlayerId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getSitTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getAddition()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getDecorationClass()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getDecorationId()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getDecorationOverTime()));
        para.put(i++, new DbParameter(Types.INTEGER, newMonopolyMktOfficeSeatData.getChangeTime()));
        if (getDb().executeNoneQuery(insertNewMonopolyMktOfficeSeatDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewMonopolyMarketOfficeSeatData parseData(ResultSet rs) throws SQLException {
        NewMonopolyMarketOfficeSeatData newMonopolyMktOfficeSeatData = new NewMonopolyMarketOfficeSeatData();
        newMonopolyMktOfficeSeatData.setWeekId(rs.getInt("weekId"));
        newMonopolyMktOfficeSeatData.setSystemGroupId(rs.getInt("systemGroupId"));
        newMonopolyMktOfficeSeatData.setSeatId(rs.getInt("seatId"));
        newMonopolyMktOfficeSeatData.setSeatLv(rs.getInt("seatLv"));
        newMonopolyMktOfficeSeatData.setPlayerId(rs.getLong("playerId"));
        newMonopolyMktOfficeSeatData.setSitTime(rs.getInt("sitTime"));
        newMonopolyMktOfficeSeatData.setAddition(rs.getInt("addition"));
        newMonopolyMktOfficeSeatData.setDecorationClass(rs.getInt("decorationClass"));
        newMonopolyMktOfficeSeatData.setDecorationId(rs.getInt("decorationId"));
        newMonopolyMktOfficeSeatData.setDecorationOverTime(rs.getInt("decorationOverTime"));
        newMonopolyMktOfficeSeatData.setChangeTime(rs.getInt("changeTime"));

        return newMonopolyMktOfficeSeatData;
    }

}
